package com.feihong.txgw.tx_precenter;

import com.blankj.utilcode.util.LogUtils;
import com.feihong.txgw.json.goodsCart.GoodsCartParser;
import com.feihong.txgw.json.goodsCart.TerminalListParser;
import com.feihong.txgw.inter.contract.shopcar.IShopCarContract;
import com.feihong.txgw.model.greendao.ShopCarGoodsModel;
import com.feihong.txgw.shopcar.bean.TerminalEntiyt;
import com.feihong.txgw.tx_model.ShopCarMainModel;
import com.feihong.txgw.utils.greendao.GreenDaoOperateHelper;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @Author wangziheng
 * @ProjectName txgw_app
 * @ClassName ShopCarMainPrecenter
 * @package com.feihong.txgw.tx_precenter
 * @data 2018/5/2 12:07
 * @Describe
 * @Change
 */
public class ShopCarMainPrecenter extends IShopCarContract.AbShopCarMainPrecenter {

    private List<ShopCarGoodsModel> mGoods;

    public static ShopCarMainPrecenter create() {
        return new ShopCarMainPrecenter();
    }

    @Override
    public void loadRecommentGoods() {
        mIModel.getRecommendGoods().subscribe(new Observer<TerminalListParser>() {
            @Override
            public void onSubscribe(Disposable d) {
                mRxManager.register(d);
            }

            @Override
            public void onNext(TerminalListParser result) {
                mIView.hideLoading();
                try {
                    List<TerminalEntiyt> terminalEntiyts = new ArrayList<>();
                    if (result.getCode() != 200 || result.getData() == null) {
                        mIView.setShopCarRecommendGoods(terminalEntiyts);
                        return;
                    }
                    mIView.setShopCarRecommendGoods(result.getData().getList());
                } catch (Exception e) {
                    mIView.setShopCarRecommendGoods(null);
                }
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                mIView.hideLoading();
                mIView.showNetErrorLayout();
            }

            @Override
            public void onComplete() {
            }
        });
    }

    @Override
    public void editShopCarGoodsToService(List<ShopCarGoodsModel> changeGoodsList) {
        mIModel.editShopCarGoodsToService(changeGoodsList, null)
                .subscribe(new Observer<GoodsCartParser>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mRxManager.register(d);
                    }

                    @Override
                    public void onNext(GoodsCartParser successResult) {
                        List<GoodsCartParser.DataBean> mData = successResult.getData();
                        if (successResult.getCode() == 200) {
                            if (mData != null && mData.size() > 0) {
                                GreenDaoOperateHelper.delAll();
                                GreenDaoOperateHelper.addGoodsCast(mData);
                            } else {
                                // 如果返回200   并且data为空   则 清空本地数据库
                                GreenDaoOperateHelper.delAll();
                            }
                        }else {
                            mIView.showErrorDialog(successResult.getMsg());
                        }
                        getShopCarGoodsFromLocal();
                        mIView.hideLoading();
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        mIView.hideLoading();
                        if (getShopCarGoodsFromLocal()) {
                            mIView.showErrorDialog("连接服务器失败，请检查网络");
                        }
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }

    @Override
    public void editShopCarGoodsToLocal(String editType, List<ShopCarGoodsModel> changeGoodsList) {
        mIModel.editShopCarGoodsToLocal(editType, changeGoodsList).subscribe();
        getShopCarGoodsFromLocal();
    }

    @Override
    public boolean getShopCarGoodsFromLocal() {
        mGoods = GreenDaoOperateHelper.getAllGoods();
        if (mGoods != null) {
            Iterator iterator = mGoods.iterator();
            while (iterator.hasNext()) {
                ShopCarGoodsModel dbGoodsCartModel = (ShopCarGoodsModel) iterator.next();
                for (int i = 0; i < mGoods.size(); i++) {
                    if (dbGoodsCartModel.getGoodsID() == mGoods.get(i).getGoodsID() && !dbGoodsCartModel.getId().equals(mGoods.get(i).getId())) {
                        LogUtils.i("通讯顾问 :" + "剔除相同商品");
                        iterator.remove();
                        break;
                    }
                }
            }
        }
        mIView.notifyAdapter(mGoods);
        if (mGoods != null && mGoods.size() != 0) {
            return true;
        }
        return false;
    }

    @Override
    public IShopCarContract.IShopCarMainModel getModel() {
        return ShopCarMainModel.create();
    }

    @Override
    public void onStart() {

    }
}
